from Lib.SerialPort import *
from Lib.crc16 import *
from Lib.Utils import *
import datetime
import time
import os
import threading

class Beam(object):
    def __init__(self,_type):
        self.listTemp1 = []
        self.gz=0
        self.zw=0
        self.gz1 = 0
        self.zw1 = 0
        self.gz2 = 0
        self.zw2= 0
        self.type=_type
        self.beamValue=0
        self.theadBeam = None
        self.theadBeamFlag = True
        self.seria = SerialPort('/dev/serial_4',9600)

    def start(self):
        self.theadBeamFlag = True
        self.theadBeam=threading.Thread(target=self.beamWork)
        self.theadBeam.start()


    def readBeam(self):
        self.seria.Write("getdatas\r\n".encode())
        encodestr = bytes.decode(self.seria.Read())
        if(encodestr!='' and encodestr  is not None):
            try:
                self.zw1 = round(float(encodestr.split(',')[0]),3)
            except :
                self.zw1 = 0

            try:
                self.gz1 = round(float(encodestr.split(',')[3]),3)
            except :
                self.gz1 = 0

            try:
                self.zw2 = round(float(encodestr.split(',')[2]),3)
            except :
                self.zw2 = 0

            try:
                self.gz2 = round(float(encodestr.split(',')[3]),3)
            except :
                self.gz2 = 0

        else:
            self.seria = SerialPort('/dev/serial_4',9600)
            time.sleep(0.2)

        if(self.type=='a'):
            self.zw=self.zw1
            self.gz=self.gz1
            return [self.zw1,self.gz1]
        else:
            self.zw=self.zw2
            self.gz=self.gz2
            return [self.zw2,self.gz2]

    def beamWork(self):
        sameCount=0
        sameValue=0
        while self.theadBeamFlag:
            try:
                time.sleep(1)
                cTem = self.readBeam()
                if(cTem[0]==sameValue):
                    sameCount+=1
                sameValue=cTem[0]
                if(sameCount>=5):
                    self.stop()
                    time.sleep(0.2)
                    self.setBeam(self.beamValue)
                    time.sleep(0.2)
                    self.setBeam(self.beamValue)
                    time.sleep(0.2)
                    self.start()
                    sameCount=0
            except:
                try:
                    self.seria = SerialPort('/dev/serial_4',9600)
                except :
                    pass



    def setBeam(self,beamValue):
        self.beamValue=beamValue
        try:
            print("setBeat set start")
            self.seria.Write(("set"+self.type+(str(beamValue).zfill(4))+'\r\n').encode())
            readStr=bytes.decode(self.seria.Read())
            print(readStr)
        except :
            self.seria = SerialPort('/dev/serial_4',9600)
            time.sleep(0.2)
            self.seria.Write(("set"+self.type+(str(beamValue).zfill(4))+'\r\n').encode())
            self.seria.Read()


    def stop(self):
        self.theadBeamFlag = False
        time.sleep(2)
        while True:
            try:
                self.seria.Write(("set"+self.type+"stop\r\n").encode())
                readStr=bytes.decode(self.seria.Read())
                print(readStr)
                if(readStr[0:8]=="set"+self.type+"stop"):
                    self.zw=0
                    self.gz=0
                    return
            except :
                try:
                    self.seria = SerialPort('/dev/serial_4',9600)
                    time.sleep(0.5)
                    self.seria.Write(("set"+self.type+"stop\r\n").encode())
                    time.sleep(0.2)
                except :
                    pass


        # self.seria.Read()

    def updateType(self,_type):
        self.type=_type



